var _n15;
var cc__extends = __extends;
var cc__decorate = __decorate;
var cc__importDefault = __importDefault;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var $1$Ipha_tool = require("Ipha_tool");
var s = cc__importDefault($1$Ipha_tool);
var $1$Ipha_ComponentBasic = require("Ipha_ComponentBasic");
var c = cc__importDefault($1$Ipha_ComponentBasic);
var cc__decorator = cc._decorator;
var ccp_ccclass = cc__decorator.ccclass;
var ccp_property = cc__decorator.property;
var ccp_menu = cc__decorator.menu;
var ccp_executeInEditMode = cc__decorator.executeInEditMode;
var def_Ipha_AutoSize = function (t) {
  function _ctor() {
    // 创建实例并设置默认属性
    const instance = null !== t && t.apply(this, arguments) || this;

    // 设置组件名称
    instance.comName = "自动使大小包含所有子节点,该组件会改变锚点"; // 组件名称

    // 初始化偏移量属性
    instance.leftOffset = 0;
    instance.rightOffset = 0;
    instance.topOffset = 0;
    instance.bottomOffset = 0;

    // 设置自动调整大小标识
    instance._isAutoSize = true;

    return instance; // 返回实例
  }
  cc__extends(_ctor, t);
  Object.defineProperty(_ctor.prototype, "isAutoSize", {
    get() {
      return this._isAutoSize;
    },
    set(t) {
      this._isAutoSize = t;
    },
    enumerable: false,
    configurable: true
  });
  _ctor.prototype.start = function () {
    if (this._isAutoSize) {
      this.autoSize();
    }
  };
  _ctor.prototype.autoSize = function () {
    const self = this; // 保存上下文的引用

    // 检查当前节点是否有子节点
    if (this.node.childrenCount > 0) {
      const minX = []; // 最小X坐标的数组
      const maxX = []; // 最大X坐标的数组
      const minY = []; // 最小Y坐标的数组
      const maxY = []; // 最大Y坐标的数组

      // 遍历子节点，读取信息
      this.node.children.forEach(function (child) {
        self.childrenRead(child, minX, maxX, minY, maxY); // 调用方法读取数据
      });

      // 计算节点的宽度和高度
      this.node.width = Math.round(100 * (Math.max(...maxX) - Math.min(...minX))) / 100 + this.leftOffset + this.rightOffset;
      this.node.height = Math.round(100 * (Math.max(...maxY) - Math.min(...minY))) / 100 + this.topOffset + this.bottomOffset;

      // 获取节点的位置
      const position = s.default.pos.getNodeWordPos(this.node);

      // 计算并设置锚点
      this.node.anchorX = (position.x - Math.min(...minX) + this.leftOffset) / this.node.width;
      this.node.anchorY = (position.y - Math.min(...minY) + this.bottomOffset) / this.node.height;
    }
  };
  _ctor.prototype.childrenRead = function (t, e, o, n, i) {
    const self = this; // 保存上下文的引用

    // 检查目标节点是否激活且有效
    if (t.active && t && t.isValid) {
      // 获取目标节点的世界坐标
      const position = s.default.pos.getNodeWordPos(t);

      // 计算目标节点的边界值并存入数组
      e.push(position.x - t.width * t.scaleX * t.anchorX); // 左边界
      o.push(position.x + t.width * t.scaleX * (1 - t.anchorX)); // 右边界
      n.push(position.y + t.height * t.scaleY * (1 - t.anchorY)); // 上边界
      i.push(position.y - t.height * t.scaleY * t.anchorY); // 下边界

      // 如果节点有子节点，则递归读取子节点数据
      if (t.childrenCount > 0) {
        t.children.forEach(function (child) {
          self.childrenRead(child, e, o, n, i); // 递归调用
        });
      }
    }
  };
  _ctor.prototype.update = function () {
    this._isAutoSize && this.autoSize();
  };
  cc__decorate([ccp_property({
    displayName: "左偏差"
  })], _ctor.prototype, "leftOffset", undefined);
  cc__decorate([ccp_property({
    displayName: "右偏差"
  })], _ctor.prototype, "rightOffset", undefined);
  cc__decorate([ccp_property({
    displayName: "上偏差"
  })], _ctor.prototype, "topOffset", undefined);
  cc__decorate([ccp_property({
    displayName: "下偏差"
  })], _ctor.prototype, "bottomOffset", undefined);
  return cc__decorate([ccp_ccclass, ccp_executeInEditMode, ccp_menu("ipha组件/自动大小")], _ctor);
}(c.default);
exports.default = def_Ipha_AutoSize;